<template>
  <CompOperate
      :field="field"
      :designer="designer"
      :design-state="designState"
      :parent-list="parentList"
      :parent-widget="parentWidget"
      :sub-form-row-id="subFormRowId"
      :sub-form-row-index="subFormRowIndex"
      :sub-form-col-index="subFormColIndex"
      :index-of-parent-list="indexOfParentList"
      :display-style="field.options.displayStyle"
  >
    <div class="scene-window-callPatient-widget" :style="getStyle('area')">
      <div class="widget-inside" :key="uuid">
        <div class="patient-list">
          <template v-for="(items, index) in waitPatientInfos">
            <div class="patient-item" :key="index" :style="getStyle('child')">
              <template v-for="(item) in optionsInfo['waitColOption']">
                <template v-if="true">
                  <template v-if="(item.value) ==='patientNo'">
                    <div class="desc-item desc-key" :class="item.value" :style="setFlex(item,'list')">
                      <div class="item-inside" :style="getStyle('child2')">{{ items['orderNo'] || '\u00a0' }}</div>
                    </div>
                  </template>

                  <template v-else-if="(item.value) ==='please'">
                    <div class="desc-item" :class="item.value" :style="setFlex(item,'list')">
                      <div class="item-inside"><i class="el-icon-user-solid"
                                                  :style="getStyle('child2')"/>{{ item['label'] || '\u00a0' }}
                      </div>
                    </div>
                  </template>

                  <template v-else-if="(item.value) ==='patientName'">
                    <div class="desc-item desc-key" :class="item.value" :style="setFlex(item,'list')">
                      <div class="item-inside" :style="getStyle('child2')">{{ items['patientName'] || '\u00a0' }}</div>
                    </div>
                  </template>

                  <template v-else-if="(item.value) ==='roomName'">
                    <div class="desc-item desc-key" :class="item.value" :style="setFlex(item,'list')">
                      <div class="item-inside" :style="getStyle('child2')">
                        {{ items['treatmentRoomName'] || '\u00a0' }}
                      </div>
                    </div>
                  </template>

                  <template v-else>
                    <div class="desc-item" :class="item.value" :style="setFlex(item,'list')">
                      <div class="item-inside">{{ item['label'] || '\u00a0' }}</div>
                    </div>
                  </template>
                </template>
              </template>
            </div>
          </template>
        </div>
        <div class="call-patient" :style="getStyle('call')">
          <ScrollX>
            {{ currentPatientInfo }}
          </ScrollX>
        </div>
      </div>
    </div>
  </CompOperate>
</template>

<script>
import {NoticeBar, Swipe, SwipeItem} from "vant";
import emitter from "@/utils/designer/emitter";
import {pxToVw} from '@/utils/common';
import ScrollY from '@c/public/seamlessScroll/ScrollY.vue';
import ScrollX from '@c/public/seamlessScroll/ScrollX.vue';
import CompOperate from "@c/public/form-designer/devType/modules/compOperate.vue";
import mixin from "@sc/templateDetails/modules/homeMiddle/fieldMixin";
import vueSeamlessScroll from 'vue-seamless-scroll';
import {v4 as uuid} from 'uuid';
import {buildParentApi} from "@/utils/designer/util";

let buildParent = buildParentApi();
export default {
  name: "scene-waiting-callPatient-widget",

  componentName: "FieldWidget", //必须固定为FieldWidget，用于接收父级组件的broadcast事件

  mixins: [emitter, mixin],

  components: {
    ScrollX,
    CompOperate,
    NoticeBar,
    Swipe,
    SwipeItem,
    ScrollY,
    vueSeamlessScroll
  },

  props: {
    widget: Object,
    devType: {
      type: Boolean,
      default: false,
    },
    field: Object,
    parentWidget: Object,
    parentList: Array,
    indexOfParentList: Number,
    designer: Object,
    designState: {
      type: Boolean,
      default: false,
    },
    subFormRowIndex: {
      /* 子表单组件行索引，从0开始计数 */ type: Number,
      default: -1,
    },
    subFormColIndex: {
      /* 子表单组件列索引，从0开始计数 */ type: Number,
      default: -1,
    },
    subFormRowId: {
      /* 子表单组件行Id，唯一id且不可变 */ type: String,
      default: "",
    },
    headTitle: {
      type: String,
      default: "",
    },
    visibleTitle: {
      type: Boolean,
    },
    //prop传入的表单JSON配置
    parentApi: {
      type: Object,
      default: () => buildParentApi()
    },
  },

  data() {
    return {
      uuid: '',
      optionsInfo: Object(),
      header: require("@a/images/design/header.jpeg"),
      pause: require("@a/images/design/pause.png"),
    }
  },

  computed: {
    // 模板配置行内样式
    getStyle() {
      return function (val) {
        const self = this;
        let info = (self.optionsInfo) || Array();
        let Obj = Object();
        switch (val) {
          case 'area':
            Reflect.set(Obj, 'height', pxToVw(info.areaHeight));
            return Obj;
          case 'head':
            Reflect.set(Obj, 'fontWeight', (info['title']['fontWeight']));
            Reflect.set(Obj, 'fontSize', pxToVw(info['title']['fontSize']));
            Reflect.set(Obj, 'color', (info['title']['color']));
            Reflect.set(Obj, 'background', (info['title']['background']));
            return Obj;
          case 'list':
            Reflect.set(Obj, 'fontSize', pxToVw(info['list']['fontSize']));
            return Obj;
          case 'orderNo':
            Reflect.set(Obj, 'color', (info['list']['orderNoColor']));
            Reflect.set(Obj, 'background', (info['list']['orderNoBackground']));
            return Obj;
          case 'patient':
            Reflect.set(Obj, 'fontWeight', (info['list']['orderNoFontWeight']));
            return Obj;
          case 'child':
            Reflect.set(Obj, 'fontWeight', info['fontWeight']);
            Reflect.set(Obj, 'fontSize', pxToVw(info['fontSize']));
            Reflect.set(Obj, 'height', pxToVw((info.areaHeight - 126 - 60 - (info.showRowNums - 1) * 6) / (info.showRowNums)));
            return Obj
          case 'child2':
            Reflect.set(Obj, 'color', info['fontColor']);
            return Obj
          case 'call':
            Reflect.set(Obj, 'color', info['callPatientFontColor']);
            Reflect.set(Obj, 'fontWeight', info['callPatientFontWeight']);
            Reflect.set(Obj, 'fontSize', pxToVw(info['callPatientFontSize']));
            return Obj
        }
      }
    },

    // 设置宽度和字号
    setFlex() {
      return function (item, val) {
        const self = this;
        let info = (self.optionsInfo) || Array();
        let Obj = Object();
        switch (val) {
          case "list":
            Reflect.set(Obj, 'fontWeight', (info['fontWeight']));
            Reflect.set(Obj, "flex", (item.flex) || 1);
            return Obj;
        }
      }
    },

    // 列表的队列的就诊
    waitPatientInfos() {
      const self = this;
      let devType = (self.devType);
      let {showRowNums} = self.optionsInfo;
      let waitPatientList = self.$store.getters['getWaitingPatientBusinessInfo'];
      let list = devType ? (waitPatientList.apiData) : (waitPatientList.mockData);
      let len = (list?.length)
      if (!!list && !!(list?.length)) {
        if (len >= showRowNums) {
          return list.slice(0, showRowNums);
        } else {
          let fillArr = Array(showRowNums - len).fill({})
          return [...list, ...fillArr]
        }
      } else {
        return Array(showRowNums).fill({});
      }
    },

    // 当前患者
    currentPatientInfo() {
      let info;
      let {waitColOption} = this.optionsInfo;
      let listInfo = this.waitPatientInfos;
      if (listInfo?.length) {
        let sliceArr = listInfo.slice(0, 1);
        let changeField = ['patientNo', 'patientName', 'roomName']
        waitColOption.forEach((item) => {
          if (changeField.includes(item['value'])) {
            if (item['value'] === 'patientNo') {
              console.log(1)
              info = `${this.$lodash.trim(info)} ${sliceArr[0].orderNo || '\u00a0'}`
            } else if (item['value'] === 'patientName') {
              console.log(2)
              info = `${this.$lodash.trim(info)} ${sliceArr[0].patientName || '\u00a0'}`
            } else if (item['value'] === 'roomName') {
              console.log(3)
              info = `${this.$lodash.trim(info)} ${sliceArr[0].treatmentRoomName || '\u00a0'}`
            }
          } else {
            info = `${this.$lodash.trim(info)} ${item.label || '\u00a0'}`
          }
        })
        return info
      } else {
        return '';
      }
    },
  },

  created() {
    this.registerToRefList();
    this.initEventHandler();
    this.handleOnCreated();
  },

  mounted() {
    this.handleOnMounted();
  },

  methods: {
    // 接收父组件传递过来的数据
    async executeMethod(key, data) {
      switch (key) {
        case "patientListInfo":
          this.uuid = uuid();
          let patientList = this.$lodash.cloneDeep(data);
          await this.$store.dispatch('setWaitingPatientBusinessInfo', patientList);
          break
      }
    },
  },

  watch: {
    field: {
      handler: function (val) {
        let key = (val.options.config);
        this.uuid = uuid();
        this.optionsInfo = (val.options[key]);
      },
      deep: true,
      immediate: true
    }
  },

  destroyed() {
    this.unregisterFromRefList();
  },

};
</script>

<style lang="scss" scoped>
.scene-window-callPatient-widget {
  color: #333;
  height: 150px;
  font-size: 40px;
  overflow: hidden;
  padding: 0 30px 16px 30px;
  box-sizing: border-box;
  background: transparent;

  .widget-inside {
    width: 100%;
    height: 100%;
    box-sizing: border-box;
    border-radius: 18px;
    overflow: hidden;
    background: rgba(0, 0, 0, 0.1);
    padding: 22px 26px;

    .patient-list {
      height: calc(100% - 126px);
      overflow: hidden;

      .patient-item {
        display: flex;
        align-items: center;
        align-self: center;
        justify-content: center;
        align-content: center;
        color: white;
        flex-direction: row;
        box-sizing: border-box;
        margin-bottom: 6px;
        font-size: 74px;

        &:first-child {
          .desc-key {
            color: #ECFF24;
          }

          .el-icon-user-solid {
            color: #ECFF24;
            padding-right: 12px;
          }
        }

        &:nth-child(even) {
          background: #0752C0;
        }

        &:nth-child(odd) {
          background: #0142A6;
        }

        &:last-child {
          margin-bottom: 0;
        }

        .desc-item {
          height: calc(100% - 20px);
          border-radius: 16px;
          overflow: hidden;
          white-space: nowrap;
          display: flex;
          align-content: center;
          align-self: center;
          align-items: center;
          justify-content: center;
          text-align: center;
        }

        .patientNo,
        .patientName,
        .roomName {
          background: #19396E;
        }

      }
    }

    .call-patient {
      width: 100%;
      margin-top: 6px;
      height: 120px;
      box-sizing: border-box;
      background: #0142A6;
      display: flex;
      align-self: center;
      align-items: center;
      justify-content: center;
      font-weight: bold;
      font-size: 90px;
      color: #ECFF24;
    }
  }
}
</style>